package com.kingre.et.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.kingre.et.Constants;
import com.kingre.et.dao.ISchoolDAO;
import com.kingre.et.dao.IUserDAO;
import com.kingre.et.model.Role;
import com.kingre.et.model.School;
import com.kingre.et.model.User;
import com.kingre.et.service.ISchoolService;

@Transactional
public class SchoolServiceImpl implements ISchoolService {
    @Autowired
    private ISchoolDAO schoolDAO;
    @Autowired
    private IUserDAO userDAO;

    public List<School> find(Integer userId, String name) {
        List<School> list = schoolDAO.findByName(name);
        if(list == null)
            return null;
        School s = null;
        User user = userDAO.get(userId);
        String roleName = user.getRole().getName();
        if(roleName.equals(Constants.TEACHER_ROLE))
        	s = user.getSchool();
        List<School> sort = new ArrayList<School>();
        for(int i = 0; i < list.size(); i++){
        	if(list.get(i).getSchoolType() == 10){
        		if(s == null || s.getId() == list.get(i).getId())
	        		sort.add(list.get(i));
	        		list.remove(i--);
        	}
        }
        for(int i = 0; i < list.size(); i++){
        	int id = list.get(i).getOverSchool().getId();
        	for(int k = 0; k < sort.size(); k++){
        		if(id == sort.get(k).getId()){
        			sort.add(k + 1, list.get(i));
        			list.remove(i--);
        			break;
        		}
        	}
        }
        for(int i = 0; i < sort.size(); i++){
        	if(sort.get(i).getSchoolType() == 10){
        		int id = sort.get(i).getId();
        		int num = 0;
        		for(int j = 0; j < sort.size(); j++){
        			if(sort.get(j).getSchoolType() == 20 && sort.get(j).getOverSchool().getId() == id){
        				int count = userDAO.findByNameAndSchool(Constants.USER_ROLE, sort.get(j), "").size();
        				if(count >= Constants.SUB_SCHOOL_REPORT_NUM)
        					sort.get(j).setStatReport(true);
        				num += count;
        			}
        		}
        		if(num >= Constants.SCHOOL_REPORT_NUM)
        			sort.get(i).setStatReport(true);
        	}
        }
        return sort;
    }

    public void delete(Integer id) {
        School s = schoolDAO.get(id);
        if(s.getSchoolType() == 10){
        	Set<School> sub = s.getUnderSchool();
        	for(School un : sub){
        		un.setStatusCode(false);
        	}
        }
        s.setStatusCode(false);
    }

    public void add(String name, String schoolType, Integer schoolId) {
        School s = new School();
        s.setName(name);
        s.setStatusCode(true);
        s.setOpenConsult(false);
        if("10".equals(schoolType))
        	s.setSchoolType(10);
        else{
        	s.setSchoolType(20);
        	School p = schoolDAO.get(schoolId);
        	p.addUnderSchool(s);
        }
        schoolDAO.save(s);
    }

    public void updateConsult(Integer id) {
        School s = schoolDAO.get(id);
        s.setOpenConsult(!s.isOpenConsult());
    }
}
